<?php
class Preferences
{
	private $preferences = array();
	private $descriptions = array();
	private $form_type = array();
	private $id, $user_id;

	
	function __construct($id)
	{
		if(empty($id) or !is_numeric($id))
			return false;
		
		$ar['id'] = $id;
		$db = new Database();
		$db->select("preferences",'*',$ar, '1');
		$data = $db->fetch();
		
		$this->id = $id;
		$this->user_id = $data['user_id'];
		
		$this->descriptions['rss_feed_url'] = "URL of the XML feed to update";
		$this->preferences['rss_feed_url'] = $data['rss_feed_url'];
		$this->form_type['rss_feed_url'] = "text";
		
		$this->descriptions['rss_category_id'] = "Notes imported from the feed belong to this category";
		$this->preferences['rss_category_id'] = $data['rss_category_id'];
		$this->form_type['rss_category_id'] = "boolean";
		
		$this->descriptions['rss_open_link_in_new_page'] = "Open RSS links in a new browser tab/window ?";
		$this->preferences['rss_open_link_in_new_page'] = $data['rss_open_link_in_new_page'];
		$this->form_type['rss_open_link_in_new_page'] = "boolean";
	}
	
	##########################
	# Loads preferences for the given user
	# Use : $prefs = Preference::load($user_id)
	# Param : user id
	# Returns : object Preference
	public static function loadForUser($user_id)
	{
		$ar['user_id'] = $user_id;
		$db = new Database();
		$db->select("preferences",'*',$ar, '1');
		$data = $db->fetch();
		return new Preferences($data['id']);
	}
	
	##########################
	# Updates the current preferences set in the database
	# Use : 1) set the variables with setter methods, 2) call this method
	# e.g. $preferences->save() // saves all fields in $note instance
	# Use : 1) set one variable with its setter, 2) call this method with the name of the field
	# e.g. : $preferences->save('title') // saves only 'title' field in $note instance
	# Param :
	# Returns :
	public function save($field = "")
	{
		// Save all fields
		if(empty($field))
		{
			foreach($this->preferences as $key->$value)
				$this->save($key);
		}
		// Save one field
		else
		{
			$data[$field] = $this->preferences[$field];
			$db = new Database();
			return $db->update("preferences",$data,"id='".$this->getId()."'");
		}
	}
	
	
	##########################
	# Prints the value of the given variable
	# Param : the name of one of the class variables (e.g. 'title' or 'body')
	# Returns :
	public function show($element)
	{
		switch($element)
		{
			case 'user_id':	echo $this->getUserId(); break;
			case 'id':		echo $this->getId(); break;
			case 'rss_feed_url':	echo $this->preferences['rss_feed_url']; break;
			case 'rss_category_id':	echo $this->preferences['rss_category_id']; break;
			case 'rss_open_link_in_new_page':	echo $this->preferences['rss_open_link_in_new_page']; break;
			default: echo ""; break;
		}
	}
	
	public function getValue($key)
	{
		return $this->preferences[$key];
	}
	
	public function setValue($key, $value)
	{
		$this->preferences[$key] = $value;
	}
	
	public function getFormType($key)
	{
		return $this->form_type[$key];
	}
	
	// Returns an array of the values of the $key preference for all the users
	public static function getAllUsersValues($key)
	{
		$db = new Database();
		$db->select("preferences",$key);
		return $db->fetch_all();
	}
	
	##########################
	# Returns an array with all preferences
	public function getAllPreferences()
	{
		return $this->preferences;
	}
	
	##########################
	# Getters
	
	public function getId() { return $this->id; }
	public function getUserId() { return $this->user_id; }
	public function getPreference($pref) { return $this->preferences[$pref]; }
	public function getDescription($pref) { return $this->descriptions[$pref]; }
}
?>
